<?php

namespace App\Http\Controllers\Api;

use App\Models\Book;
use App\Models\BookPublishInfo;
use App\Models\User;
use Faker\Factory;
use Illuminate\Http\Request;
use Faker\Generator as Faker;
use Carbon\Carbon;
use DB;
use EasyWeChat;

class TestController extends BaseController
{
    public function index()
    {
        $list = Book::query()->select('id')->where('image','=','')->where('deleted',0)->get();

        if(!empty($list))
        {
            foreach ($list as $v)
            {
                //echo $v->id;
                DB::table('books')->where('id',$v->id)->update([
                    'deleted' => 1,
                ]);
                //die;
            }
        }


        echo 'ok';
        die;
        //$list = Book::query()->select('id','douban_score','real_user_read_num')->offset(19000)->limit(2000)->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                if($v->douban_score <= 0)
                {
                    $number = mt_rand(1,15);
                }
                if($v->douban_score > 0 && $v->douban_score <= 7.4)
                {
                    $number = mt_rand(15,50);
                }

                if($v->douban_score > 7.4 && $v->douban_score <= 10)
                {
                    $number = mt_rand(51,120);
                }

                //echo $v->id;
                $total_read_num = $v->real_user_read_num+$number;
                DB::table('books')->where('id',$v->id)->update([
                    'system_user_read_num' => $number,
                    'total_read_num' => $total_read_num
                ]);
                //die;
            }
        }

        echo 'ok';


        die;


        $result = getOrderTracesByJson('SF','037079399615');
        $result = json_decode($result, true);
        print_r($result);
        die;
        $sms = app('easysms');
        try {
            $sms->send('18260165847',[
                'template' => 'SMS_142615314',
                'data' => [
                    'name' => 'hello'
                ],
            ]);
            echo 'ok';
        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
            $message = $exception->getException('aliyun')->getMessage();
            dd($message);
        }
        die;
        $app = EasyWeChat::officialAccount(); // 公众号
        $buttons = [
            [
                "type" => "miniprogram",
                "name" => "我要借书",
                "key"  => "",
                "appid" => "wx85852aca65f9b19e",
                "pagepath" => "/pages/index/index"
            ],
            [
                "name"       => "借书订单",
                "sub_button" => [
                    [
                        "type" => "miniprogram",
                        "name" => "待收货",
                        "url"  => "",
                        "appid" => "wx85852aca65f9b19e",
                        "pagepath" => "pages/my_order/my_order?state=1"
                    ],
                    [
                        "type" => "miniprogram",
                        "name" => "待归还",
                        "url"  => "pages/my_order/my_order?state=2",
                        "appid" => "wx85852aca65f9b19e",
                        "pagepath" => ""
                    ],
                    [
                        "type" => "miniprogram",
                        "name" => "已归还",
                        "key" => "pages/my_order/my_order?state=3",
                        "appid" => "wx85852aca65f9b19e",
                        "pagepath" => ""
                    ],
                ],
            ],
            [
                "type" => "miniprogram",
                "name" => "人工客服",
                "key"  => ""
            ],
        ];
        $app->menu->create($buttons);
        echo 'ok';
        die;

        //$response = $officialAccount->server->serve();
        //return $response;


        $list = BookPublishInfo::query()->where('id','>',17967)->get()->toArray();
        foreach ($list as $v)
        {
            $arr = explode('/',$v['publish_time']);
            if(count($arr)==3)
            {
                $time = $arr[2].'/'.$arr[0].'/'.$arr[1];
                DB::table('book_publish_infos')->where('id',$v['id'])->update([
                    'publish_time'=>$time
                ]);
            }

        }


        die;


        /*$sms = app('easysms');
        try {
            $sms->send(18260165847,[
                'template' => 'SMS_85380002',
                'data' => [
                    'number' => 1234
                ],
            ]);
        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
            $message = $exception->getException('aliyun')->getMessage();
            dd($message);
        }*/
    }

    public function pay()
    {
        $order = [
            'out_trade_no' => time(),
            'total_fee' => 1, // **单位：分**
            'body' => 'test body - 测试',
            'openid' => 'oyYnM4sAhyrnlQfKEcJYVyVwjsdU',
        ];

        $result = app('wechat_pay')->miniapp($order);
        return $this->res([
            'data' => $result
        ]);
    }

    //生成假数据用户
    public function users(Faker $faker)
    {
        //file_get_contents(resource_path('avatarurl.txt'));
        $avatar_urls_arr = [];
        $file_arr = file(resource_path('avatarurl.txt'));
        for($i=0;$i<count($file_arr);$i++){     //逐行读取文件内容
            $avatar_urls_arr[] = $file_arr[$i];
        }
        //print_r($avatar_urls_arr);
        $wechat_nicknames_arr = [];
        $file_arr = file(resource_path('wechat_nickname.txt'));
        for($i=0;$i<count($file_arr);$i++){     //逐行读取文件内容
            $wechat_nicknames_arr[] = $file_arr[$i];
        }

        // 随机取一个月以内的时间
        $updated_at = $faker->dateTimeThisMonth();
        // 传参为生成最大时间不超过，创建时间永远比更改时间要早
        $created_at = $faker->dateTimeThisMonth($updated_at);

        $num = count($avatar_urls_arr);

        for ($i = 1999;$i < $num;$i++)
        {
            /*if($i == 1999)
            {
                break;
            }*/
            DB::table('users')->insert([
                'user_type' => 0,
                'wechat_nickname' => str_replace("\n","",$wechat_nicknames_arr[$i]),
                'wechat_openid' => str_random(32),
                'wechat_avatar' => str_replace("\r\n","",$avatar_urls_arr[$i]),
                'created_at' => $created_at,
                'updated_at' => $updated_at,
            ]);
        }

        echo 'ok';
        //print_r($wechat_nicknames_arr);
    }
}
